package com.companion.lh.app

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.companion.lh.app.model.Position
import com.companion.lh.companion_app.R
import kotlinx.android.synthetic.main.activity_activiry.*

class ActivityActivity : AppCompatActivity() {

    private var destinationPosition: Position? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_activiry)
        supportActionBar?.setTitle(R.string.text_activity_create)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        destinationAddressEdit.setOnClickListener { toSearchPlacePage() }
    }

    private fun toSearchPlacePage() {
        val intent = Intent(this, SearchPlaceActivity::class.java)
        startActivityForResult(intent, INTENT_ID_DESTINATION)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (resultCode) {
            INTENT_ID_DESTINATION -> {
                val bundle = data?.extras
                destinationPosition = bundle?.get("position") as Position
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            android.R.id.home -> {
                finish()
                true
            }
            else -> {
                return super.onOptionsItemSelected(item)
            }
        }
    }

    companion object {
        const val INTENT_ID_DESTINATION = 1
    }
}
